<?php
//uncomment when moved to server - to ensure this page is not accessed from anywhere else
//if ($_SERVER['REMOTE_ADDR'] !== '<your server ip address') {
//  die("You are not a cron job!");
//}

  require_once 'app/Mage.php';
  umask(0);

  //$_SERVER['SERVER_PORT']='443';
  Mage::app('admin');

  //$params = Mage::app()->getRequest()->getParam('files'); // set the filename that is to be imported - file needs to be present in var/import directory  
  
  Mage::log("*****************************************************************************");  
  Mage::log("Lightspeed Update Started");   
  if(Mage::getStoreConfig('lightspeed/backend/in_progress')) 
  {
  	echo 'Lightspeed Update Still in Progress';
  	Mage::log("Lightspeed Update Aborted, Previous Update Still in Progress, updates: {$updated}, imports: {$imported}");
  	die();
  	exit();
  }
  Mage::getModel('core/config')->saveConfig('lightspeed/backend/in_progress', true);
  $userModel = Mage::getModel('admin/user');
  $userModel->setUserId(0);
  Mage::getSingleton('admin/session')->setUser($userModel);
  $batchModel = Mage::getModel('lightspeed/lightspeed');   
  
   //Delete Removed Items
  Mage::log("Start Product Cleanup");
  $batchModel->cleanupProducts();
  Mage::log("Finish Product Cleanup");
  
  //Tidy Up Categories
  Mage::log("Start Category Cleanup");
  $batchModel->cleanupCategories();
  Mage::log("Finish Category Cleanup");
  
  //Import New Since Last
  $rowIds = $batchModel->getImportIds();
  $lastImport = Mage::getStoreConfig('lightspeed/backend/last_import');
  $rowCount = count($rowIds);
  Mage::log("Last Import at {$lastImport} found {$rowCount} New Prodcuts");
  
  $imported=0;
  foreach ($rowIds as $importId) {               

                try {
                	//Get Lightspeed Data
                	$lsProductArray=$batchModel->getProducts($importId);
                	//Save LightSpeed Data
                	$lsProduct=$batchModel->saveProducts($lsProductArray);
                	$messages[]=$lsProduct;
                }
                catch (Exception $e) {
                    $errors = $e->getMessage();
                    Mage::log("Lightspeed Cron Error on Import".$errors);
                    continue;
                }
                $imported ++;
    }
    Mage::getModel('core/config')->saveConfig('lightspeed/backend/last_import', date('Y-m-d H:i:s') );
	
	Mage::log("Set Last Import to ".date('Y-m-d H:i:s'));
  	
	//Update Updated Since Last Time
  	//Get Update Ids
  	$rowIds = $batchModel->getUpdateIds();
	$lastUpdate = Mage::getStoreConfig('lightspeed/backend/last_update');
	$rowCount = count($rowIds);
	Mage::log("Last Update at {$lastUpdate} found {$rowCount} Updated Prodcuts");
  	$updated=0;
  	foreach ($rowIds as $importId) {               

                try {
                	//Get Lightspeed Data
                	$lsProductArray=$batchModel->getProducts($importId);
                	//Save LightSpeed Data
                	$lsProduct=$batchModel->updateProducts($lsProductArray);
                	$messages[]=$lsProduct;
                }
                catch (Exception $e) {
                    $errors = $e->getMessage();
                    Mage::log("Lightspeed Cron Error on Update".$errors);
                    continue;
                }
                $updated ++;
    }
  	Mage::getModel('core/config')->saveConfig('lightspeed/backend/last_update', date('Y-m-d H:i:s') );
	Mage::log("Set Last Update to ".date('Y-m-d H:i:s'));
  
  echo 'Lightspeed Update Completed';
  Mage::log("Lightspeed Update Completed, updates: {$updated}, imports: {$imported}");
  Mage::log("*****************************************************************************");  
  Mage::getModel('core/config')->saveConfig('lightspeed/backend/in_progress', false);
 ?>